package com.sh.personnel.security.config.handler;

import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 未登录返回的信息
 * @author 林思浩
 * @date 2020/01/05 18:58
 */
@Component
public class MyAuthEntryPoint extends MyCustomHandler implements AuthenticationEntryPoint {

    private Object code = HttpServletResponse.SC_UNAUTHORIZED; //401
    private String message;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException {
        System.out.println(e);
        if(e instanceof LockedException){
            message = "账户被锁定";
        } else if(e instanceof BadCredentialsException){
            message = "账户名或密码错误";
        } else if(e instanceof DisabledException){
            message = "账户被禁用";
        } else if(e instanceof AccountExpiredException){
            message = "账户已过期";
        } else if(e instanceof CredentialsExpiredException){
            message = "密码已过期";
        } else {
            code = HttpServletResponse.SC_FOUND; //302
            message = "您尚未登录或登录信息失败，请重新登录！";
        }
        this.responseJson(code, message, response);
    }

}
